package com.kezesong.leasing.controller.admin;


import com.kezesong.leasing.common.ResponseResult;
import com.kezesong.leasing.controller.BaseController;
import com.kezesong.leasing.controller.request.admin.AddGoodsInfoReqBody;
import com.kezesong.leasing.controller.request.admin.ModifyGoodsInfoReqBody;
import com.kezesong.leasing.entity.GoodsInfoEntity;
import com.kezesong.leasing.service.GoodsInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class GoodsInfoManagerController extends BaseController {

    @Autowired
    private GoodsInfoService goodsInfoService;


    @PostMapping("/getGoodsList.do")
    @ApiOperation(value = "getGoodsList", notes = "管理员获取全部商品类别列表")
    public ResponseResult<List<GoodsInfoEntity>> getGoodsList(){
        List<GoodsInfoEntity> goodsInfoList = goodsInfoService.getGoodsInfoList();
        if (null == goodsInfoList || goodsInfoList.size() == 0){
            return buildNoDataResponse();
        }else {
            ResponseResult<List<GoodsInfoEntity>> listResponseResult = buildSucessResponse(goodsInfoList);
            return listResponseResult;
        }
    }

    @GetMapping("/getGoodsListByCategory/{categoryId}/pageSize/pageIndex")
    @ApiOperation(value = "getGoodsListByCategory", notes = "管理员获取某个类别的商品")
    public ResponseResult<List<GoodsInfoEntity>> getGoodsList(@PathVariable("categoryId") int categoryId,
                                                              @PathVariable("pageSize") int pageSize,
                                                              @PathVariable("pageIndex") int pageIndex){
        List<GoodsInfoEntity> goodsInfoList = goodsInfoService.getGoodsInfoList(categoryId, pageSize, pageIndex);
        if (null == goodsInfoList || goodsInfoList.size() == 0){
            return buildNoDataResponse();
        }else {
            ResponseResult<List<GoodsInfoEntity>> listResponseResult = buildSucessResponse(goodsInfoList);
            return listResponseResult;
        }
    }

    @PostMapping("/addGoodsInfo.do")
    @ApiOperation(value = "addGoodsInfo", notes = "管理员添加商品")
    public ResponseResult<String> addGoodsInfo(@RequestBody AddGoodsInfoReqBody reqBody){
        GoodsInfoEntity goodsInfoEntity = new GoodsInfoEntity();
        goodsInfoEntity.setGoodsCategoryId(reqBody.getCateGoryId());
        goodsInfoEntity.setGoodsName(reqBody.getGoodsName());
        goodsInfoEntity.setPicture(reqBody.getPicture());
        goodsInfoEntity.setPrice(reqBody.getPrice());
        goodsInfoEntity.setGoodsFlag(reqBody.getGoodsFlag());
        goodsInfoEntity.setShowOrder(reqBody.getShowOrder());
        goodsInfoEntity.setGoodsDescribe(reqBody.getGoodsDescribe());
        int rows = goodsInfoService.addGoodsInfo(goodsInfoEntity);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }

    @GetMapping("/deleteGoodsInfo/{goodsId}")
    @ApiOperation(value = "deleteGoodsInfo", notes = "删除商品")
    public ResponseResult<String> deleteCategory(@PathVariable("goodsId") int goodsId){
        int rows = goodsInfoService.deleteGoodsInfo(goodsId);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }


    @PostMapping("/modifyGoodsInfo.do")
    @ApiOperation(value = "modifyGoodsInfo", notes = "修改商品")
    public ResponseResult<String> modifyGoodsInfo(@RequestBody ModifyGoodsInfoReqBody reqBody){
        GoodsInfoEntity goodsInfoEntity = new GoodsInfoEntity();
        BeanUtils.copyProperties(reqBody,goodsInfoEntity);
        int rows = goodsInfoService.modifyGoodsInfo(goodsInfoEntity);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }




}
